package com.wenqiner.requesttool.view.base;

import com.wenqiner.requesttool.domain.OriginRequestJsonData;
import com.wenqiner.requesttool.domain.ParallelStatistics;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 并发
 * @author rhf
 * @date 2023-03-30
 **/
public interface ParallelController {
    /**
     * 并发参数
     * @author rhf
     * @param requestJsonData 参数
     * @param index 下标
     */
    void request(OriginRequestJsonData requestJsonData, Integer index);

    /**
     * 请求响应信息
     * @author rhf
     * @param parallelStatistics 请求数
     * @return java.lang.String
     */
    default String printResInfo(ParallelStatistics parallelStatistics){
        String resultAnalyse = "总耗时{0}毫秒\n" +
                "总请求数:{1}\n" +
                "发送http请求成功数:{2}\n" +
                "发送http请求失败数:{3}\n" +
                "发送http请求成功qps:{4}\n" +
                "发送http请求qps:{5}\n" +
                "发送http成功率:{6}\n" +
                "完结时间:\n" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(parallelStatistics.getFinishTimestamp()));
        long runTimestamp = parallelStatistics.getFinishTimestamp() - parallelStatistics.getStartTimestamp();
        long total = parallelStatistics.getTotal();
        long succ = parallelStatistics.getSuccessSize();
        long fail = parallelStatistics.getFailureSize();
        BigDecimal runSecond = BigDecimal.valueOf(runTimestamp).divide(BigDecimal.valueOf(1000),6, RoundingMode.HALF_UP);

        BigDecimal totalQps = BigDecimal.valueOf(total).divide(runSecond, 2, RoundingMode.HALF_UP);
        BigDecimal succQps = BigDecimal.valueOf(succ).divide(runSecond, 2, RoundingMode.HALF_UP);
        BigDecimal succRate = BigDecimal.valueOf(succ).divide(BigDecimal.valueOf(total), 5, RoundingMode.HALF_UP);
        return MessageFormat.format(resultAnalyse
                , runTimestamp
                , total
                , succ
                , fail
                , succQps
                , totalQps
                , succRate
        );
    }
}
